package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.doamin.PdfUser;
import com.itheima.doamin.cargo.Export;
import com.itheima.doamin.cargo.ExportProduct;
import com.itheima.doamin.cargo.ExportProductExample;
import com.itheima.service.cargo.ExportProductService;
import com.itheima.service.cargo.ExportService;
import com.itheima.web.controller.BaseController;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.*;

@Controller
@RequestMapping("cargo/export")
public class PdfController extends BaseController{
    @Reference
    ExportService exportService;
    @Reference
    ExportProductService exportProductService;

//    @RequestMapping("exportPdf1")
//    public void exportPdf1() throws Exception {
//        //获得Jasper文件的绝对路径
//        String pdfRealPath=session.getServletContext().getRealPath("/jesper/test2.jasper");
//        //获得jasperprint对象
//        Map<String,Object> userMap=new HashMap<>();
//        userMap.put("username","huang");
//        userMap.put("email","boke_huang@foxmail.com");
//        userMap.put("username","hhao");
//        userMap.put("email","boke@foxmail.com");
//        JasperPrint jasperPrint = JasperFillManager.fillReport(pdfRealPath,userMap, new JREmptyDataSource());
//        //进行导出
//        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
//
//    }

//    @RequestMapping("exportPdf1")
//    public void exportPdf1() throws Exception {
//        //获得Jasper文件的绝对路径
//        String pdfRealPath=session.getServletContext().getRealPath("/jesper/test3.jasper");
//        //获得数据库连接对象
//        Class.forName("com.mysql.jdbc.Driver");
//        //
//        Connection con= DriverManager.getConnection("jdbc:mysql:///saas-export","root","q631406926");
//        JasperPrint jasperPrint = JasperFillManager.fillReport(pdfRealPath,new HashMap<>(), con);
//        //进行导出
//        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
//
//    }

//    @RequestMapping("exportPdf1")
//    public void exportPdf1() throws Exception {
//        //获得Jasper文件的绝对路径
//        String pdfRealPath=session.getServletContext().getRealPath("/jesper/test3.jasper");
//        //获得数据库连接对象
//        Class.forName("com.mysql.jdbc.Driver");
//        //
//        Connection con= DriverManager.getConnection("jdbc:mysql:///saas-export","root","q631406926");
//        JasperPrint jasperPrint = JasperFillManager.fillReport(pdfRealPath,new HashMap<>(), con);
//        //进行导出
//        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
//
//    }

    //报表分组查询
//    @RequestMapping("exportPdf")
//    public void exportPdf() throws Exception {
//        //获得Jasper文件的绝对路径
//        String pdfRealPath=session.getServletContext().getRealPath("/jesper/test6.jasper");
//        List pdfCompany=new ArrayList<>();
//        for (int i=0;i<3;i++){
//           Map map=new HashMap();
//           map.put("companyName","haohao");
//           map.put("dept","mydept"+i);
//           map.put("phone","电话"+i);
//           pdfCompany.add(map);
//        }
//
//
//        for (int i=3;i<6;i++){
//            Map map=new HashMap();
//            map.put("companyName","caocao");
//            map.put("dept","mydept"+i);
//            map.put("phone","电话"+i);
//            pdfCompany.add(map);
//        }
//
//        for (int i=6;i<9;i++){
//            Map map=new HashMap();
//            map.put("companyName","huang");
//            map.put("dept","mydept"+i);
//            map.put("phone","电话"+i);
//            pdfCompany.add(map);
//        }
//        JasperPrint jasperPrint = JasperFillManager.fillReport(pdfRealPath,new HashMap<>(),new JRBeanCollectionDataSource(pdfCompany));
//        //进行导出
//        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
//
//    }

    @RequestMapping("exportPdf")
    public void exportPdf(String id) throws Exception {
        //获得Jasper文件的绝对路径
        String pdfRealPath=session.getServletContext().getRealPath("/jesper/export.jasper");
        //获取报运合同
        Export export = exportService.findById(id);
        //获得其下的货物
        //创建条件对象
        ExportProductExample exportProductExample=new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProductServiceAll = exportProductService.findAll(exportProductExample);
        //将对象装为map
        BeanMap beanMap=new BeanMap(export);
        Set set = beanMap.keySet();
        Map exportMap=new HashMap();
        for (Object key:set){
            exportMap.put(key,beanMap.get(key));
        }

        JasperPrint jasperPrint = JasperFillManager.fillReport(pdfRealPath,exportMap,new JRBeanCollectionDataSource(exportProductServiceAll));
        //进行导出
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }


    public Connection getConnecton() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con= DriverManager.getConnection("jdbc:mysql:///saas-export","root","q631406926");
        return con;
    }

}
